﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DSkin.Forms;
using System.Security.Cryptography;
using QQLogin.Properties;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;

namespace QQLogin
{
    public partial class FrmMain : Skin_VS_Light
    {
        private string userName, password;
        private ISmsPlatform smsPlatform;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (btnLogin.Text == "退出")
            {
                btnLogin.Text = "登录";
                return;
            }
            userName = txtUser.Text.Trim();
            password = txtPass.Text.Trim();
            var yumi = rbtnYuMi.Checked;
            if (userName == "" || password == "")
            {
                return;
            }
            ThreadPool.QueueUserWorkItem(s =>
            {
                if (yumi)
                {
                    smsPlatform = new YuMiPlatform { Pid = 3274 };
                    if (!smsPlatform.Login(userName, password, out string msg))
                    {
                        Invoke(new Action(() =>
                        {
                            DSkinMessageBox.Show(msg);
                        }));
                        return;
                    }
                }
                else
                {
                    //极速
                }
                Invoke(new Action(() =>
                {
                    btnLogin.Text = "退出";
                }));
            });
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            var txts = txtInput.Lines;
            foreach (var txt in txts)
            {
                if (txt.Trim() == "")
                    continue;
                var data = Regex.Split(txt, "----", RegexOptions.IgnoreCase);
                if (data.Length != 2)
                    continue;
                var index = dgvQQ.Rows.Add();
                dgvQQ[0, index].Value = true;
                dgvQQ[1, index].Value = data[0];
                dgvQQ[2, index].Value = data[1];
            }
        }

        private void ShowMsg(string msg, int index)
        {
            Invoke(new Action(() => dgvQQ[3, index].Value = msg));
        }

        private void 反向选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvQQ.RowCount == 0)
                return;
            for (int i = dgvQQ.RowCount - 1; i >= 0; i--)
            {
                var flag = Convert.ToBoolean(dgvQQ[0, i].Value);
                dgvQQ[0, i].Value = !flag;
            }
        }

        private void 删除选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvQQ.RowCount == 0)
                return;
            for (int i = dgvQQ.RowCount - 1; i >= 0; i--)
            {
                if (Convert.ToBoolean(dgvQQ[0, i].Value))
                {
                    dgvQQ.Rows.RemoveAt(i);
                }
            }
        }

        private void btnHaoLaiwu_Click(object sender, EventArgs e)
        {
            if (dgvQQ.RowCount == 0)
            {
                DSkinMessageBox.Show("请导入账号");
                return;
            }
            ThreadPool.QueueUserWorkItem(s =>
            {
                for (var i = 0; i < dgvQQ.RowCount; i++)
                {
                    if (!Convert.ToBoolean(dgvQQ[0, i].Value))
                        continue;
                    if (DoWork(dgvQQ[1, i].Value.ToString(), dgvQQ[2, i].Value.ToString(), i))
                    {
                        Invoke(new Action(() => dgvQQ[0, i].Value = false));
                    }
                }
            });
        }

        private bool DoWork(string qq, string pass, int index)
        {
            ShowMsg("正在登录", index);
            var helper = new QQLoginHelper(qq, pass);
            var code = "";
            var flag = true;
            for (var a = 0; a < 5; a++)
            {
                flag = helper.IsNeedCode(ref code);
                if (!flag) break;
                Thread.Sleep(1000);
            }
            if (flag)
            {
                ShowMsg("登录尝试超时，请先手动登录一次", index);
                return false;
            }
            var res = helper.Login(code);
            ShowMsg(res, index);
            var msg = "";
            if (!res.Contains("成功")) return false;
            if (!helper.Check())
            {
                ShowMsg("已经绑定!", index);
                return true;
            }
            SmsModel model = null;
            var phone = "";
            while (true)
            {
                model = smsPlatform.GetMobileNum();
                if (!model.Success)
                {
                    ShowMsg(model.Message, index);
                    return false;
                }
                phone = model.Data;
                ShowMsg($"手机号获取成功：{phone}", index);
                if (!helper.GetCode(phone, out msg))
                {
                    ShowMsg(msg, index);
                    if (msg == "手机已经绑定过")
                    {
                        smsPlatform.AddIgnoreList(phone);
                        continue;
                    }
                    return false;
                }
                break;
            }
            ShowMsg("验证码发送成功", index);
            ShowMsg("正在获取验证码...", index);
            for (int i = 0; i < 20; i++)
            {
                model = smsPlatform.GetCode(phone);
                if (!model.Success)
                {
                    ShowMsg($"正在获取验证码...{i}", index);
                    Thread.Sleep(3000);
                    continue;
                }
                break;
            }
            if (!model.Success)
            {
                smsPlatform.AddIgnoreList(phone);
                ShowMsg(model.Message, index);
                return false;
            }
            var smsCode = new Regex("【腾讯视频】(.*?) 腾讯视频VIP验证码").Match(model.Data).Groups[1].Value;
            ShowMsg($"验证码获取成功：{smsCode}", index);
            if (!helper.Bind(phone, smsCode, out msg))
            {
                ShowMsg(msg, index);
                return false;
            }
            ShowMsg(msg, index);
            return true;
        }

        private void 清空列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvQQ.Rows.Clear();
        }
    }
}
